package com.yanqu.road.dao.impl.player;

import com.yanqu.road.dao.db.DbNameStrategy;
import com.yanqu.road.dao.db.DbParameter;
import com.yanqu.road.dao.impl.TempDataObjectDao;
import com.yanqu.road.entity.player.UserMail;
import com.yanqu.road.utils.string.StringUtils;

import java.sql.*;
import java.util.HashMap;
import java.util.List;
import java.util.Map;
import java.util.concurrent.ConcurrentHashMap;

public class UserMailDaoImpl extends TempDataObjectDao<UserMail> {

	public Map<String, UserMail> getUserMailMap(long userId) {
		String sql = "select * from t_u_mail where receiveUserId=? and isDelete=0";
		Map<String, UserMail> dataMap = new ConcurrentHashMap<>();
		ResultSet rs = null;
		CallableStatement call = null;
		try {
			getDb().setConnName(DbNameStrategy.getUserDb());
			call = getDb().getConnection().prepareCall(sql);
			call.setLong(1, userId);
			rs = call.executeQuery();
			while (rs.next()) {
				UserMail data = parse(rs);
				dataMap.put(data.getMailUid(), data);
			}
		} catch (Exception e) {
			throw new RuntimeException(e);
		} finally {
			getDb().closeConnection(call, rs);
		}
		return dataMap;
	}

	public UserMail getUserMailByUid(String mailUId) {
		ResultSet rs = null;
		CallableStatement call = null;
		String sql = "select * from t_u_mail where mailUid=?";
		try {
			getDb().setConnName(DbNameStrategy.getUserDb());
			call = getDb().getConnection().prepareCall(sql);
			call.setString(1, mailUId);
			rs = call.executeQuery();
			while (rs.next()) {
				UserMail data = parse(rs);
				return data;
			}
		} catch (Exception e) {
			throw new RuntimeException(e);
		} finally {
			getDb().closeConnection(call, rs);
		}
		return null;
	}

	@Override
	protected boolean addData(UserMail data) {
		PreparedStatement call = null;
		String sql = "INSERT INTO t_u_mail(mailUid,mailType,title,content,annexes,sendTime,sendUserId,sendNickName,receiveUserId,"
				+ "isRead,readTime,isGetReward,isDelete,serverList,activityId,personalId,qrCode) VALUES(?,?,?,?,?,?,?,?,?,?,?,?,?,?,?,?,?)";
		try {
			getDb().setConnName(DbNameStrategy.getUserDb());
			call = getDb().getConnection().prepareStatement(sql);
			int i = 1;
			call.setString(i++, data.getMailUid());
			call.setInt(i++, data.getMailType());
			call.setString(i++, data.getTitle());
			call.setString(i++, data.getContent());
			call.setString(i++, data.getAnnexes());
			call.setTimestamp(i++, new Timestamp(data.getSendTime().getTime()));
			call.setLong(i++, data.getSendUserId());
			call.setString(i++, data.getSendNickName());
			call.setLong(i++, data.getReceiveUserId());
			call.setBoolean(i++, data.isRead());
			call.setTimestamp(i++, new Timestamp(data.getReadTime().getTime()));
			call.setBoolean(i++, data.isGetReward());
			call.setBoolean(i++, data.isDelete());
			call.setString(i++, data.getServerList());
			call.setInt(i++, data.getActivityId());
			call.setLong(i++, data.getPersonalId());
			call.setString(i++, data.getQrCode());
			return call.executeUpdate() > 0;
		} catch (Exception e) {
			logger.error(Thread.currentThread().getStackTrace()[1].getMethodName() + ":", e);
		} finally {
			getDb().closeConnection(call, null);
		}
		return false;
	}

	@Override
	protected boolean updateData(UserMail data) {
		PreparedStatement call = null;
		String sql = "UPDATE t_u_mail SET mailType=?,title=?,content=?,annexes=?,sendTime=?,sendUserId=?,sendNickName=?,receiveUserId=?,"
				+ "isRead=?,readTime=?,isGetReward=?,qrCode=?,isDelete=? WHERE mailUid=?";
		try {
			getDb().setConnName(DbNameStrategy.getUserDb());
			call = getDb().getConnection().prepareStatement(sql);
			int i = 1;
			call.setInt(i++, data.getMailType());
			call.setString(i++, data.getTitle());
			call.setString(i++, data.getContent());
			call.setString(i++, data.getAnnexes());
			call.setTimestamp(i++, new Timestamp(data.getSendTime().getTime()));
			call.setLong(i++, data.getSendUserId());
			call.setString(i++, data.getSendNickName());
			call.setLong(i++, data.getReceiveUserId());
			call.setBoolean(i++, data.isRead());
			call.setTimestamp(i++, new Timestamp(data.getReadTime().getTime()));
			call.setBoolean(i++, data.isGetReward());
			call.setString(i++, data.getQrCode());
			call.setBoolean(i++, data.isDelete());
			call.setString(i++, data.getMailUid());
			return call.executeUpdate() > 0;
		} catch (Exception e) {
			logger.error(Thread.currentThread().getStackTrace()[1].getMethodName() + ":", e);
		} finally {
			getDb().closeConnection(call, null);
		}
		return false;
	}

	public boolean deleteUserMail(String mailUid) {
		String sql = "DELETE FROM t_u_mail WHERE mailUid=?";
		try {
			getDb().setConnName(DbNameStrategy.getUserDb());
			Map<Integer, DbParameter> para = new HashMap<>();
			para.put(1, new DbParameter(Types.VARCHAR, mailUid));
			return getDb().executeNoneQuery(sql, para) > -1;
		} catch (Exception e) {
			logger.error(Thread.currentThread().getStackTrace()[1].getMethodName() + ":", e);
		}
		return false;
	}

	public boolean deleteUserMailList(List<String> mailUidList) {
		String sql = "DELETE FROM t_u_mail WHERE mailUid in(\"" + StringUtils.listToString(mailUidList, "\",\"") + "\")";
		try {
			getDb().setConnName(DbNameStrategy.getUserDb());
			Map<Integer, DbParameter> para = new HashMap<>();
			return getDb().executeNoneQuery(sql, para) > -1;
		} catch (Exception e) {
			logger.error(Thread.currentThread().getStackTrace()[1].getMethodName() + ":", e);
		}
		return false;
	}

	@Override
	protected UserMail parseData(ResultSet rs) throws SQLException {
		UserMail data = new UserMail();
		data.setMailUid(rs.getString("mailUid"));
		data.setMailType(rs.getShort("mailType"));
		data.setTitle(rs.getString("title"));
		data.setContent(rs.getString("content"));
		data.setAnnexes(rs.getString("annexes"));
		data.setSendTime(rs.getTimestamp("sendTime"));
		data.setSendUserId(rs.getLong("sendUserId"));
		data.setSendNickName(rs.getString("sendNickName"));
		data.setReceiveUserId(rs.getLong("receiveUserId"));
		data.setRead(rs.getBoolean("isRead"));
		data.setReadTime(rs.getTimestamp("readTime"));
		data.setDelete(rs.getBoolean("isDelete"));
		data.setGetReward(rs.getBoolean("isGetReward"));
		data.setDelete(rs.getBoolean("isDelete"));
		data.setServerList(rs.getString("serverList"));
		data.setActivityId(rs.getInt("activityId"));
		String qrCode = rs.getString("qrCode");
		data.setQrCode(qrCode ==null?"": qrCode);
		return data;
	}
}
